package com.ctshk.rpc.bus.mapper;

import java.util.Set;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.common.req.MainTreeData;
import com.ctshk.common.req.OperationByLikeReq;
import com.ctshk.common.service.mapper.CtsBaseMapper;
import com.ctshk.common.service.mapper.CtsQueryWrapper;
import com.ctshk.common.service.mapper.CtsSQL;
import com.ctshk.common.utils.Assist;
import com.ctshk.rpc.bus.dto.BusStopsDTO;
import com.ctshk.rpc.bus.entity.BusStops;
import com.ctshk.rpc.bus.req.BusStopsDictListQueryReq;
import com.ctshk.rpc.bus.req.BusStopsPassDictListQueryReq;
import com.ctshk.rpc.bus.req.BusStopsQueryReq;

/**
 * <p>
 * 巴士站点表 Mapper 接口
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-02-08
 */
public interface BusStopsMapper extends CtsBaseMapper<BusStops> {

	/**
     * 查询巴士站点数据字典列表
     * @param req
     * @return
     */
	@SelectProvider(type = BusStopsMapper.class, method = "queryStopsDictListProvider")
	IPage<MainTreeData> queryStopsDictList(IPage<?> page, @Param("req") BusStopsDictListQueryReq req);
	
	static String queryStopsDictListProvider(BusStopsDictListQueryReq req) {
		return new CtsSQL() {{
			SELECT_DISTINCT("t.id as id, t.site_name as name");
			FROM("t_bus_stops t");
			WHERE("t.is_deleted = 0 and t.status = 0 and t.site_name is not null");
			IF_NOT_BLANK_LIKE(req.getName(), "t.site_name", "req.name");
			IF_NOT_BLANK_LIKE(req.getCityName(), "t.city_name", "req.cityName");
			GROUP_BY("t.site_name");
		}}.toString();
	}
	
	/**
     * 查询巴士经停站站点数据字典列表
     * @param req
     * @return
     */
	@SelectProvider(type = BusStopsMapper.class, method = "queryStopsPassDictListProvider")
	IPage<MainTreeData> queryStopsPassDictList(IPage<?> page, @Param("req") BusStopsPassDictListQueryReq req);
	
	static String queryStopsPassDictListProvider(BusStopsPassDictListQueryReq req) {
		return new CtsSQL() {{
			SELECT_DISTINCT("t.id as id, t.site_name as name");
			FROM("t_bus_stops t");
			WHERE("t.is_deleted = 0 and t.status = 0 and t.site_name is not null");
			IF_NOT_BLANK_LIKE(req.getName(), "t.site_name", "req.name");
			
			boolean isDepartureCityName = Assist.isNotBlank(req.getDepartureCityName());
			boolean isArriveCityName = Assist.isNotBlank(req.getArriveCityName());
			
			if (isDepartureCityName && isArriveCityName) {
				WHERE("(t.city_name like concat('%', #{req.departureCityName}, '%') or t.city_name like concat('%', #{req.arriveCityName}, '%') )");
			} else {
				IF_NOT_BLANK_LIKE(req.getDepartureCityName(), "t.city_name", "req.departureCityName");
				IF_NOT_BLANK_LIKE(req.getArriveCityName(), "t.city_name", "req.arriveCityName");
			}
			GROUP_BY("t.site_name");
		}}.toString();
	}
	
	/**
	 * 检索巴士城市列表
	 * @param page
	 * @param req
	 * @return
	 */
	@SelectProvider(type = BusStopsMapper.class, method = "searchCityListProvider")
	IPage<MainTreeData> searchCityList(IPage<?> page, @Param("req") OperationByLikeReq req);
	
	static String searchCityListProvider(OperationByLikeReq req) {
		return new CtsSQL() {{
			SELECT_DISTINCT("t.city_id as id, t.city_name as name");
			FROM("t_bus_stops t");
			WHERE("t.is_deleted = 0 and t.status = 0 and t.city_name is not null");
			IF_NOT_BLANK_LIKE(req.getName(), "t.city_name", "req.name");
			GROUP_BY("t.city_name");
		}}.toString();
	}
	
	/**
	 * 查询路由整体信息
	 * @return 格式：城市名称_站点名称
	 */
	@Select("select concat(ifnull(t.city_name, ''), '_', ifnull(t.site_name, '')) from t_bus_stops t where t.is_deleted = 0")
	Set<String> queryRouteInfoList();
	
	/**
     * 查询巴士站点列表
     */
	default IPage<BusStopsDTO> queryStopsList(BusStopsQueryReq req) {
		CtsQueryWrapper<BusStops> wrapper = newCtsWrapper()
				.ifNotNullEq("status", req.getStatus())
				.ifNotNullEq("city_id", req.getCityId())
				.ifNotNullEq("supplier_id", req.getSupplierId())
				.ifNotBlankLike("site_name", req.getSiteName())
				.ifNotBlankLike("city_name", req.getCityName())
				.ifNotBlankLike("supplier_name", req.getSupplierName());
		return selectPage(BusStopsDTO.class, req, wrapper);
	}
}
